package com.suwenhao.controller;


import com.alibaba.fastjson.JSONObject;
import com.suwenhao.constants.Constant;
import com.suwenhao.entity.User;
import com.suwenhao.enums.SessionEnum;
import com.suwenhao.service.UserService;
import com.suwenhao.utils.MdUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/22 17:35
 */

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {
    private UserService userService = new UserService();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //验证账号和密码
        //TODO
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String remember = req.getParameter("remember");
        String code = req.getParameter("code");

        User user = userService.checkLogin(username, MdUtil.md5(password));

        HttpSession session = req.getSession();


        //验证码
//        Object obj = session.getAttribute(SessionEnum.SESSION_PIC_CODE.getValue());

//        if (!code.equalsIgnoreCase(obj.toString())) {
//            //验证码不正确
//            resp.sendRedirect("/index.jsp");
//            return;
//        }


        if (user != null) {
            //账号密码正确登录成功

            //把登录信息存放到session中
            session.setAttribute(Constant.SESSION_LOGIN, user);

            session.setMaxInactiveInterval(30 * 60);

            //判断是否选中免登录
            if ("1".equals(remember)) {
                //选中 把登录信息存放到cookie里面
                String s = JSONObject.toJSONString(user);
                String encode = URLEncoder.encode(s, "utf-8");
                Cookie cookie = new Cookie(Constant.COOKIE_LOGIN, encode);

                cookie.setMaxAge(7 * 24 * 60 * 60);
                cookie.setPath("/");
                resp.addCookie(cookie);

            }

            req.setAttribute("user", (User) session.getAttribute(Constant.SESSION_LOGIN));
            req.getRequestDispatcher("/html/common/home.jsp").forward(req, resp);
        } else {
            //账号密码错误登录失败
            resp.sendRedirect("/index.jsp");
            System.out.println("登录失败");
        }


    }

    protected void forget(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        Object obj = req.getSession().getAttribute(SessionEnum.SESSION_FORGET.getValue());

        if(obj==null || !obj.toString().equals(code) ){
            //没有验证码或者验证码不正确
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }

        //修改密码
        User user=new User();
        user.setUsername(username);
        user.setPassword(MdUtil.md5(password));

        userService.updatePassword(user);

        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }
}


